﻿using System;
using System.Collections.Generic;
using System.Text;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.Core.Units;
using LBoL.EntityLib.Cards.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeFeedbackDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = new ManaGroup() { Blue = 2 ,Any=1 };
            config.UpgradedCost = new ManaGroup() { Blue = 1 };
            config.AutoPerform = false;

            config.Value1 = 5;
            config.Value2 = 8;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.RelativeCards = new List<string>() { "ProbeFeedbackDeployed" };
            config.UpgradedRelativeCards = new List<string>() { "ProbeFeedbackDeployed" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 能量反馈
    /// 部署静置结界
    /// 部署完成在手中：
    ///     回合开始时，取消部署并弃掉该牌。
    ///     若敌人为自己施加正面状态，移除该状态并造成3/5倍于层数的伤害,随后转化成未部署状态
    /// </summary>
    [EntityLogic(typeof(ProbeFeedbackDef))]
    public sealed class ProbeFeedback : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => false;

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            //  消耗自身，给一张已部署的结界
            yield return new AddCardsToHandAction(Library.CreateCard<ProbeFeedbackDeployed>(IsUpgraded));
            yield return new RemoveCardAction(this);
        }
    }

    public sealed class ProbeFeedbackDeployedDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;
            config.IsPooled = false;
            config.Revealable = true;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = ManaGroup.Empty;
            config.UpgradedCost = ManaGroup.Empty;
            config.AutoPerform = true;

            config.Keywords = Keyword.Retain | Keyword.Forbidden;
            config.UpgradedKeywords = Keyword.Retain | Keyword.Forbidden;

            config.Value1 = 5;
            config.Value2 = 8;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 已部署能量反馈
    /// 部署静置结界
    /// 部署完成在手中：
    ///     回合开始时，取消部署并弃掉该牌。
    ///     若敌人为自己施加正面状态，移除该状态并造成3/5倍于层数+持续时间的伤害,随后转化成未部署状态
    /// </summary>
    [EntityLogic(typeof(ProbeFeedbackDeployedDef))]
    public sealed class ProbeFeedbackDeployed : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => true;

        protected override void OnEnterBattle(BattleController battle)
        {
            ReactBattleEvent(base.Battle.Player.TurnStarted, OnPlayerTurnStarting);

            //  监听所有活着的敌人
            foreach(var enemy in battle.AllAliveEnemies)
            {
                ReactBattleEvent(enemy.StatusEffectAdded, OnTrigger);
            }

            
        }

        private IEnumerable<BattleAction> OnTrigger(StatusEffectApplyEventArgs args)
        {
            if (args.Effect.Type == StatusEffectType.Positive && args.Unit != Battle.Player)
            {
                //  敌人buff自身就移除，失去对应层数+持续时间的5/10倍伤害
                if (base.Zone == CardZone.Hand)
                {
                    int damageIndex = IsUpgraded?Value2 : Value1;

                    int count = 0;

                    if (args.Effect.HasLevel)
                    {
                        count += args.Level.Value;
                    }

                    if (args.Effect.HasDuration)
                    {
                        count += args.Duration.Value;
                    }

                    yield return new RemoveStatusEffectAction(args.Effect);
                    yield return new DamageAction(args.Unit, args.Unit,DamageInfo.HpLose((count) * damageIndex));

                    yield return new AddCardsToDiscardAction(Library.CreateCard<ProbeStasisWard>(IsUpgraded));
                    yield return new RemoveCardAction(this);
                }
            }
        }

        private IEnumerable<BattleAction> OnPlayerTurnStarting(UnitEventArgs args)
        {
            if (!base.Battle.BattleShouldEnd && base.Zone == CardZone.Hand)
            {
                // 回合开始，消耗自身，转化成目标卡牌
                yield return new AddCardsToDiscardAction(Library.CreateCard<ProbeFeedback>(IsUpgraded));
                yield return new RemoveCardAction(this);
            }
        }

    }
}
